package com.lgj.mall.product.service.impl;

import com.alibaba.nacos.shaded.io.grpc.netty.shaded.io.netty.util.internal.StringUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.lgj.mall.common.utils.PageUtils;
import com.lgj.mall.common.utils.Query;
import com.lgj.mall.product.dao.AttrAttrgroupRelationDao;
import com.lgj.mall.product.entity.AttrAttrgroupRelationEntity;
import com.lgj.mall.product.entity.AttrEntity;
import com.lgj.mall.product.service.AttrAttrgroupRelationService;
import com.lgj.mall.product.service.AttrService;
import com.lgj.mall.product.vo.AttrGroupWithAttrsVO;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.lgj.mall.product.dao.AttrGroupDao;
import com.lgj.mall.product.entity.AttrGroupEntity;
import com.lgj.mall.product.service.AttrGroupService;


@Service("attrGroupService")
public class AttrGroupServiceImpl extends ServiceImpl<AttrGroupDao, AttrGroupEntity> implements AttrGroupService {

    @Autowired
    private AttrService attrService;
    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<AttrGroupEntity> page = this.page(
                new Query<AttrGroupEntity>().getPage(params),
                new QueryWrapper<AttrGroupEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public PageUtils queryPage(Map<String, Object> params, Long catelogId) {
        String key = (String) params.get("key");
        QueryWrapper<AttrGroupEntity> wrapper = new QueryWrapper<>();

        if (!StringUtils.isEmpty(key)) {
            wrapper.and(obj -> {
                obj.eq(StringUtils.isNumeric(key), "attr_group_id", key)  // 仅在数字时才 eq
                        .or().like("attr_group_name", key);
            });
        }

        if (catelogId != 0) {
            wrapper.eq("catelog_id", catelogId);
        }

        IPage<AttrGroupEntity> page = this.page(
                new Query<AttrGroupEntity>().getPage(params),
                wrapper
        );

        return new PageUtils(page);

    }
    //先获取分类下的所有属性分组 以及关联的所有属性(子孩子)
    @Override
    public List<AttrGroupWithAttrsVO> getAttrGroupWithAttrs(Long catelogId) {
        //1.获取所有三级分类下对应的属性组
        LambdaQueryWrapper<AttrGroupEntity> lambdaQueryWrapper = new LambdaQueryWrapper<AttrGroupEntity>().eq(AttrGroupEntity::getCatelogId, catelogId);
        List<AttrGroupEntity> lists = this.list(lambdaQueryWrapper);
        //2.获取所有属性组下的属性(然后填充)
        List<AttrGroupWithAttrsVO> collect = lists.stream().map((group) -> {
            AttrGroupWithAttrsVO vo = new AttrGroupWithAttrsVO();
            BeanUtils.copyProperties(group, vo);
            //3.获取属性组对应的属性信息,设置进去
            List<AttrEntity> relationAtrr = attrService.getRelationAtrr(group.getAttrGroupId());
            vo.setAttrs(relationAtrr);
            return vo;
        }).collect(Collectors.toList());
        return collect;
    }
}

